var eventData = {ids:null,len:0,index:0,status:"init",eid:""};
var swfu;
$(function(){
	init();
}); 

function init(){
	getevent();
	init_dialog();
	//swfupload_configure();
	init_picture_div_btn();
	t();
}

t = function(){
	$(".label_wall").click(function(){
		$("#eid").val(eventData.eid);
		$("#submitform").click();
	});
}

pausepic = function(arg){
		
	var gallerydiv = $(arg).parent().parent().parent();
	var ispause = $(arg).attr("ispause");
	
	cb = function(data){			
		if(data.code==1){
			if(ispause==1){
				gallerydiv.removeClass("gallerydiv");
				gallerydiv.addClass("gallerydivpause");
			}else{
				gallerydiv.removeClass("gallerydivpause");
				gallerydiv.addClass("gallerydiv");
			}			
			ispause = ispause==1 ? 2 :1;
			$(arg).attr("ispause",ispause);
		}
	}
	var json = {url:site.web+"my/gallery/handle",fid:$(arg).attr("fid"),eid:eventData.eid,fn:$(arg).attr("act"),ispause:ispause};
	G(json,cb);		
}

delpic = function(arg){
	var parentdiv = $(arg).parent().parent().parent();
	parentdiv.css("background","#FF0033");
	var flag = confirm("是否删除图片?");
	parentdiv.css("background","none");
	if(flag){
		cb = function(data){			
			if(data.code==1){
				parentdiv.parent().remove();				
			}
		}
		var json = {url:site.web+"my/gallery/handle",fid:$(arg).attr("fid"),eid:eventData.eid,fn:$(arg).attr("act")};
		G(json,cb);		
	}
}

init_picture_div_btn = function(){
}


getevent = function(){
	//获取活动信息
	if(eventData.status=="init"){
		eventData.status = "get";
		cb = function (data){
			if(data.code==1){
				eventData.ids = data.data;
				eventData.len = data.data.length;
				$(".gallery_list").empty();
				$(".gallery_list").html("<span class='label'>努力加载中...<span>");
				showphoto();
			}else{
				alert("ERROR gallery 404");
			}
		}
		var url = site.web+"my/event/geteventbyuser";
		var json = {url:url,uid:site.uid};
		G(json,cb);
	}
	
}

showphoto = function(){
	if(eventData.ids){
		$("#dialog_open").attr("disabled",false);
		$(".gallery_list").empty();
		$(".gallery_list").html("<span class='label'>努力加载中...<span>");
		
		cb = function (data){
			if(data.code==1){
				var photo = data.data.photo,
					event = data.data.event,
					ulist = "";
				div = function(clas,key){
					return "<div class='"+clas+"'>"+key+"</div>";
				}
				img = function(src){
					return "<img data-src='holder.js/150x150' style='width: 150px; height:150px;' src='"+src+"'/>";
				}
				caption = function(source,desc,fid,status){
					var value = "",
					 	active ="",
					 	ispause = status;//1:not pause,2:pause
					if(status==2){
						active = "active";
					}
					if(!desc)desc="&nbsp;";
					value += "<p class='margin10tb'>"+desc+"</p>";
					value += "<div class='btn-group captionbtns' style='display:none;position: absolute;margin-top:-75px;'>";
					value += "<a target='blank' class='btn btn-info btn-mini' href='"+site.web+"files/"+source+"' title='查看原图'>原图</a>";
					value += "<a target='blank' class='btn btn-info btn-mini "+active+"' onclick='pausepic(this)' fid='"+fid+"' ispause='"+ispause+"' act='pause' title='停止在活动中显示该图片'  data-toggle='button'>暂停</a>";
					value += "<a target='blank' class='btn btn-info btn-mini'  onclick='delpic(this)' fid='"+fid+"' act='del' title='删除图片'>删除</a>";
					value += "</div>";
					return value;
				}
				
				photodiv = function(src,desc,source,fid,status){
					var value = "";
					value += "<li class='span2'>";
					if(status==1)
					value += "<div class='thumbnail margin5 gallerydiv'>";
					if(status==2)
						value += "<div class='thumbnail margin5 gallerydivpause'>";
					
					value += img(src);
					value += div("caption",caption(source,desc,fid,status));
					value += "</div>"
					value += "</li>";
					return value;
				}
				//set image
				if(photo.length!=0){
					$.each(photo,function(index,value){
						var path = site.web+photo[index]['fileimg'],
							desc = photo[index]['filedesc'],
							source = photo[index]['filepath'],
							status = photo[index]['status'];
							fid = photo[index]['fid'];
//						ulist += "<li class='span2'><a class='thumbnail' title='"+desc+"'>"+img(path)+"</a></li>";
						ulist += photodiv(path,desc,source,fid,status);
					});
					$(".thumbnail").live("mouseenter",function(){
						$(this).find(".captionbtns").show();
					});
					$(".thumbnail").live("mouseleave",function(){
						$(this).find(".captionbtns").hide();
					});
					
					$(".gallery_list").empty();
					
					$(".gallery_list").append("<span class='label label-info' style='margin:10px 0 0 -25px;'>"+event.title+"图片:"+photo.length+"张<span>");
					$(".gallery_list").append("<ul class='thumbnails'>"+ulist+"</ul>");
				}else{
					$(".gallery_list").empty();
					$(".gallery_list").html("<span class='label'>"+event.title+"暂时没有活动照片哟!<span>");
				}
				
				//set event
				$(".label_name").text(event.title);
				$(".label_date").text("活动时间:"+timeformat(event.startdate,4)+" ~ "+timeformat(event.enddate,6));
				$(".label_wall").attr("title",event.title);
			}else{
				alert("ERROR gallery 404");
			}
		}
		var url = site.web+"my/gallery/get";
		eventData.eid = eventData.ids[eventData.index];
		var json = {url:url,uid:site.uid,eid:eventData.eid};
		//从0 开始加载 
		if(eventData.index==eventData.len-1){
			eventData.index = 0;
		}else{
			eventData.index ++;
		}
		G(json,cb);		
	}
}

init_dialog = function(){
	$("#dialog_open").click(function(){//create & open dialog		
		swfupload_configure(eventData.eid);		
		$('#myModal').modal('show');		
	});
	
	$(".canceldialog").click(function(){//cancel		
		swfu.destroy();
		$('#myModal').modal('hide');
		window.location = site.web + "my/gallery";
	});
	
}

swfupload_configure = function(eid){
	//属性赋值[1]
	var furl 	= site.web + "js/swfupload/swfupload.swf",
		uurl 	= site.web + "mobile/service/uploadeventfiles";
		btnurl 	= site.web + "skin/img/swfupload.png",
		ssid 	= site.ssid,
		uid 	= site.uid;
	//setting设置[2]
	var settings = {
		flash_url :furl ,
		upload_url: uurl,
		post_params: {"PHPSESSID" : ssid,"uid":uid,"eid":eid},
		file_size_limit : "1000 MB",
		file_types : "*.*",
		file_types_description : "All Files",
		file_upload_limit : 50,  
		file_queue_limit : 0,
		custom_settings : {
			progressTarget : "fsUploadProgress",
			cancelButtonId : "btnCancel"
		},
		debug:false,

		// Button settings
		button_image_url: btnurl,
		button_width: "60",
		button_height: "25",
		button_placeholder_id: "spanButtonPlaceHolder",
		button_text: '<span class="theFont">浏览</span>',
		button_text_style: ".theFont { font-size: 14px; color:#ffffff; cursor:pointer;}",
		button_text_left_padding: 14,
		button_text_top_padding: 2,
		
		file_queued_handler : fileQueued,
		file_queue_error_handler : fileQueueError,
		file_dialog_complete_handler : fileDialogComplete,
		upload_start_handler : uploadStart,
		upload_progress_handler : uploadProgress,
		upload_error_handler : uploadError,
		upload_success_handler : uploadSuccess,
		upload_complete_handler : uploadComplete,
		queue_complete_handler : queueComplete	
	};
	//new SWFupload[3]
	swfu = new SWFUpload(settings);
}
